package com.py.credit.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.constant.RecordStatusEnum;
import com.py.credit.dao.CreditViewServiceDao;
import com.py.credit.dao.SSearchDao;
import com.py.credit.entity.DataResult;
import com.py.credit.entity.TableResult;
import com.py.credit.service.CreditViewService;

import io.swagger.models.auth.In;
@Service
public class CreditViewServiceImpl implements CreditViewService {
	@Autowired
	private CreditViewServiceDao creditViewServicedao;
	
	@Autowired
	private SSearchDao sSearchDao;


	@Override
	public List<Map<String, Object>> getTableList() {
		List<Map<String, Object>>  list = new ArrayList<Map<String,Object>>();
		//获取所有的表
		List<Map<String, String>> tables = creditViewServicedao.gettables();
		//先清表在插入
		creditViewServicedao.deletetableDataCount();
		//获取每个表中数据量
		for(Map map :tables) {
			
			creditViewServicedao.inserttableDataCount(map.get("table_resource").toString(),map.get("tablename").toString(),map.get("id").toString());
		}
		//归集数据总量
		String dataconllectionNum = creditViewServicedao.getdataconllectionNum();
		//前十表
		List <Map<String, String>> tablecount = creditViewServicedao.selecttablecount();
	
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("dataconllectionNum", dataconllectionNum);
		map.put("tablecount", tablecount);
		list.add(map);
		
		return list;
	}
	@Override
	public List<Map<String, Object>> getdataNum() {
		List<Map<String, Object>>  list = new ArrayList<Map<String,Object>>();
		//获取所有的表
		List<Map<String, String>> tables = creditViewServicedao.gettables();
		//信息资源数
		Integer tableCount = tables.size();
		//获取归集总数
		String allDataCounts = creditViewServicedao.getdataconllectionNum();
				//自然人数量
				String  allZrrCounts = creditViewServicedao.getallZrrCounts();
				//法人数量
				String  allFrCounts = creditViewServicedao.getallFrCounts();
				//非企业法人数量
				String  allOtherCounts = creditViewServicedao.getallOtherCounts();
				//获取所有部门
				String deptcount = creditViewServicedao.getdeptcount();
				Map<String, Object> map = new HashMap<String, Object>();
				//获取所有错误数
				String allerrCounts  = creditViewServicedao.getallerrCounts();
				//部门采集Top5
				List<Map<String,String>>  deptTop5 =creditViewServicedao.getDeptTop5();
				//部门归集动态
				List<Map<String,String>>  deptdynamic =creditViewServicedao.getdeptdynamic();
				//合格率
				String Qualified =  creditViewServicedao.qualified();
				map.put("deptcount", deptcount);
				map.put("tableCount", String.valueOf(tableCount));
				map.put("allDataCounts", allDataCounts);
				map.put("allerrCounts", allerrCounts);
				map.put("allZrrCounts", allZrrCounts);
				map.put("allFrCounts", allFrCounts);
				map.put("Qualified", String.valueOf(Qualified));
				map.put("deptTop5", deptTop5);
				map.put("deptdynamic", deptdynamic);
				list.add(map);
				
				return list;
	}


	@Override
	public List<Map<String, String>> getcreditWarnData() {
		List<Map<String, String>>  list = new ArrayList<Map<String,String>>();
		Map<String, String> map = new HashMap<String, String>();
		//预警数量
		String warnNum = creditViewServicedao.getwarnNum();
		
		//每月预警数量
		List<Map<String,String>> warnMonthNum = creditViewServicedao.getwarnMonthNum();
		map.put("warnNum", warnNum);
		list.add(map);
		for(Map monthNum:warnMonthNum) {
			list.add(monthNum);
		}
		return list;
	}
	@Override
	public List<Object> getcreditviewSelect(String tybm, String kId, String keyWord) {
		List<Object> list = new ArrayList<Object>();
		List<TableResult> tableList = creditViewServicedao.selectSsearchResource(kId, "YZS");

		Map<String,Integer> map = new HashMap<>();
		Set<String> categorySet = new HashSet<String>();
		int count = 0;
		for(int i=0;i<tableList.size();i++) {
			String categoryName = tableList.get(i).getCategoryName();
			categorySet.add(categoryName);
			

			if(!categorySet.contains(categoryName)) {
				categorySet.add(categoryName);
		}
		}
		//获取所有类
				List<String> lists = new ArrayList<>(categorySet);
				//将所有类转Map<Object,List<>>
				  Map<Object, List<Object>> maps = lists.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.toList()));
				 for(String key :lists) {
					 //生成的map中key对应的value值清除
					 maps.get(key).clear();
				 }
				 Map<String,Object> map1 =  new HashMap<String,Object>();	
				 if (!CollectionUtils.isEmpty(tableList)) {			
					for (TableResult table : tableList) {	
						List<DataResult> datas = table.getData();
						String tablename = "t_"+table.getTableNameEN();
						String type = table.getCategoryName();	
						for(Object key:maps.keySet()) {
							if(type.equals(key)) {
								//行政处罚判断公示截至日期
								if(tablename.endsWith("t_1000000010002_XSni4zSzP0")) {
									//获取对应表的数据
									List<Object> datalist = sSearchDao.getXzcfList(tablename,keyWord);	
									//二级类map
								//	map1.put(table.getTableNameCN(), datalist);
									map1.put("size", datalist.size());
									maps.get(key).add(map1);					
									map1 = new HashMap<String,Object>();	
								}else {
									//获取对应表的数据
									List<Object> datalist = sSearchDao.getDataList(tablename,keyWord);	
									//二级类map
								//map1.put(table.getTableNameCN(), datalist);
									map1.put("size", datalist.size());
									maps.get(key).add(map1);					
									map1 = new HashMap<String,Object>();	
								}				
								
							}
						}	
				}
				}
				Map<String,Object> result = new HashMap<>();		
				for(String key :lists) {
					result.put(key, maps.get(key));
				}		
		list.add(result);
		//获取企业信息行业类型及状态
		Map<String,String> hylist = creditViewServicedao.gethylist(keyWord);
		list.add(hylist);
		return list;
	}
	@Override
	public List<Map<String,Long>>checkfromPlatformFr(String ztmc, String bm, String fddbr, String fddbrzjhm, String tablename) {
		
		return creditViewServicedao.checkfromPlatformFr(ztmc,bm,fddbr,fddbrzjhm,tablename);
	}
	@Override
	public List<Map<String,Long>>checkfromPlatformZrr(String ztmc, String bm, String tablename) {
		
		return creditViewServicedao.checkfromPlatformZrr(ztmc,bm,tablename);
	}

	@Override
	public List<Object> getAllLonAndLat() {
		
		return creditViewServicedao.getAllLonAndLat();
	}
	
	/* (非 Javadoc)
	 *
	 
	 *
	 
	 * @return
	 * @see com.py.credit.service.CreditViewService#getHyFb()
	 */
	
	@Override
	public List<Object> getHyFb() {
		return creditViewServicedao.getHyFb();
	}
	
	/* (非 Javadoc)
	 *
	 
	 *
	 
	 * @return
	 * @see com.py.credit.service.CreditViewService#dataSyCollection()
	 */
	
	@Override
	public Object dataSyCollection() {
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH )+1;
		String data = "";
		if(month<10) {
			data = year+"-0"+month;
		}else {
			data = year+"-"+month;
		}
		
		//获取当前月数据归集总量、自然人数据归集量、法人数据归集量、非企业法人数据归集量
		List  monthlist = creditViewServicedao.getdataCollectionByMonth(data);
		//获取当前月各部门数据归集总量、自然人数据归集量、法人数据归集量、非企业法人数据归集量
		List  deptlist = creditViewServicedao.getdataCollectionByMonthandDept(data);
		Map<String,Object> map = new HashMap<>();
		map.put("monthlist", monthlist);
		map.put("deptlist", deptlist);
		return map;
	}
	
	/* (非 Javadoc)
	 *
	 
	 *
	 
	 * @return
	 * @see com.py.credit.service.CreditViewService#qyOverview()
	 */
	
	@Override
	public Map<String,String> getqyOverview(String tybm) {
		
		return creditViewServicedao.getqyOverview(tybm);
	}



	
}
